﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
// Date: 24/01/2009
// Time: 16:00
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: ${FILE_NAME}.cs
#endregion
using System;
using System.Drawing;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;

namespace csharp.personal.rts.entries
{
	/// <summary>
	/// Description of frmRegisterUser.
	/// </summary>
	public partial class frmRegisterUser : csharp.common.RegisterTemplate
	{
		private User user = new User();
		private Users listUser = new Users();
		
		public frmRegisterUser()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		
		#region overrided methods
		protected override bool delete()
		{
			return user.delete();
		}
		
		protected override void fillDataGridView()
		{
			int tam;
			
			if (!listUser.fillList())
			{
				MessageBox.Show("ERRO: "+listUser.errorMessage());
				return;
			}
			dgvMain.Rows.Clear();
			tam = listUser.count();
			for (int i=0; i < tam; i++)
			{
				dgvMain.Rows.Add(new String[] {listUser.get(i).id,
				                 	listUser.get(i).realName});
			}
		}
		
		protected override void find()
		{
			csharp.common.Util.searchInGrid(dgvMain, "name", txtSearch.Text);
		}
		
		protected override void init()
		{
			DataGridViewTextBoxColumn id = new DataGridViewTextBoxColumn();
			DataGridViewTextBoxColumn name = new DataGridViewTextBoxColumn();
			
			id.Visible  = false;
			id.Name		= "id";
			
			name.Name   	 = "name";
			name.HeaderText = "Nome";
			name.Width 		 = 300;
			
			dgvMain.Columns.AddRange(new DataGridViewColumn[] {id, name});
		}
		
		protected override void newEvent()
		{
			txtName.Focus();
		}
		
		protected override bool save()
		{
			return user.save();
		}
		
		protected override void setClass()
		{
			user.realName = txtName.Text;
			user.name	  = txtUsername.Text;
			user.pass	  = txtPass.Text;
			user.email    = txtEmail.Text;
			user.active   = cbxActive.Checked;
			
			if (user.id.Equals(String.Empty))
				user.createDate = DateTime.Now.ToString("dd/MM/yyyy");
		}
		
		protected override void setEdits()
		{
			user.id = dgvMain.CurrentRow.Cells["id"].Value.ToString();
			
			if (user.get())
			{
				txtName.Text 		= user.realName;
				txtUsername.Text 	= user.name;
				txtPass.Text		= user.pass;
				txtEmail.Text		= user.email;
				cbxActive.Checked	= user.active;
			}
		}
		
		protected override bool update()
		{
			return user.update();
		}
		
		#endregion
	}
}
